package com.zhuwhao.sample;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;

import com.gjiazhe.wavesidebar.WaveSideBar;
import com.zhuwhao.sample.entity.Contacts;
import com.zhuwhao.sample.utils.AppUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SideBarActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @BindView(R.id.waveSideBar)
    WaveSideBar waveSideBar;

    private CommonAdapter<Contacts> adapter;

    private List<Contacts> contactsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_side_bar);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        toolbar.setTitle(R.string.side_bar);
        setSupportActionBar(toolbar);

        adapter = new CommonAdapter<Contacts>(this, R.layout.item_contacts, contactsList) {
            @Override
            protected void convert(ViewHolder holder, Contacts contacts, final int position) {
                if (position == 0 || !contactsList.get(position - 1).getIndex().equals(contacts.getIndex())) {
                    holder.setVisible(R.id.textIndex, true);
                    holder.setText(R.id.textIndex, contacts.getIndex());
                } else {
                    holder.setVisible(R.id.textIndex, false);
                }
                holder.setText(R.id.textName, contacts.getName());
                holder.getView(R.id.textName).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        AppUtils.showToast(SideBarActivity.this, contactsList.get(position).getName());
                    }
                });
            }
        };
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        waveSideBar.setOnSelectIndexItemListener(new WaveSideBar.OnSelectIndexItemListener() {
            @Override
            public void onSelectIndexItem(String index) {
                for (int i = 0; i < contactsList.size(); i++) {
                    if (contactsList.get(i).getIndex().equals(index)) {
                        ((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(i, 0);
                        return;
                    }
                }
            }
        });
    }

    private void initData() {
        contactsList.add(new Contacts("猪八戒"));
        contactsList.add(new Contacts("沙僧"));
        contactsList.add(new Contacts("常羲"));
        contactsList.add(new Contacts("朱雀"));
        contactsList.add(new Contacts("嫦娥"));
        contactsList.add(new Contacts("二郎神"));
        contactsList.add(new Contacts("伏羲"));
        contactsList.add(new Contacts("孙悟空"));
        contactsList.add(new Contacts("观世音"));
        contactsList.add(new Contacts("精卫"));
        contactsList.add(new Contacts("夸父"));
        contactsList.add(new Contacts("女娲"));
        contactsList.add(new Contacts("哪吒"));
        contactsList.add(new Contacts("祝融"));
        contactsList.add(new Contacts("盘古"));
        contactsList.add(new Contacts("青龙"));
        contactsList.add(new Contacts("如来"));
        contactsList.add(new Contacts("顺风耳"));
        contactsList.add(new Contacts("太白金星"));
        contactsList.add(new Contacts("太上老君"));
        contactsList.add(new Contacts("羲和"));
        contactsList.add(new Contacts("玄武"));
        contactsList.add(new Contacts("白虎"));

        //排序
        Collections.sort(contactsList, new Comparator<Contacts>() {
            @Override
            public int compare(Contacts c1, Contacts c2) {
                return c1.getIndex().compareTo(c2.getIndex());
            }
        });
        adapter.notifyDataSetChanged();

        //设置WaveSideBar的IndexItems
        List<String> indexItems = new ArrayList<>();
        for (int i = 0; i < contactsList.size(); i++) {
            if (i == 0 || !contactsList.get(i - 1).getIndex().equals(contactsList.get(i).getIndex())) {
                indexItems.add(contactsList.get(i).getIndex());
            }
        }
        waveSideBar.setIndexItems(indexItems.toArray(new String[indexItems.size()]));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
